package chabernac.android.wordbattle.model;

import java.util.List;


public class OneDirectionOnlySelectionHelper implements iSelectionHelper {

  @Override
  public boolean isSelectionAllowed(WordRaster aRaster, Letter aNewLetterToSelect) {
    List<Letter> theSelectedLetters = aRaster.getSelectedLetters();
    if(!theSelectedLetters.isEmpty()){
      Letter theLastSelectedLetter = theSelectedLetters.get(theSelectedLetters.size() - 1);
      if(Math.abs(theLastSelectedLetter.getColumn() - aNewLetterToSelect.getColumn()) > 1) return false;
      if(Math.abs(theLastSelectedLetter.getRow() - aNewLetterToSelect.getRow()) > 1) return false;
    }
    
    if(theSelectedLetters.size() >= 2){
      int theAllowedColumnOffset = theSelectedLetters.get(1).getColumn() - theSelectedLetters.get(0).getColumn();
      int theAllowedRowOffset = theSelectedLetters.get(1).getRow() - theSelectedLetters.get(0).getRow();
      
      Letter theLastSelectedLetter = theSelectedLetters.get(theSelectedLetters.size() - 1);
      if(aNewLetterToSelect.getColumn() - theLastSelectedLetter.getColumn() != theAllowedColumnOffset) return false;
      if(aNewLetterToSelect.getRow() - theLastSelectedLetter.getRow() != theAllowedRowOffset) return false;
    }
    
    return true;
  }

  @Override
  public boolean isDiagonalSelectionGapNeeded() {
    return false;
  }

}
